const Sequelize = require('sequelize');
const mysql2 = require('mysql2');

const config = require('./config');

const sequelize = new Sequelize(config.database, config.username, config.password, {
    host: config.host,
    dialect: 'mysql',
    dialectModule: mysql2,
    // logging: false,
    timezone: '+08:00',
    pool: {
        max: 5,
        min: 0,
        idle: 30000
    }
});

const Gateway = sequelize.define('Gateway', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    name: Sequelize.STRING,
    type: Sequelize.STRING,
    updatedAt: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'gateway'
});

const Device = sequelize.define('Device', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    mac: Sequelize.STRING,
    type: Sequelize.STRING,
    // updatedAt: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'device'
});

const HeartRateDetail = sequelize.define('hr_detail', {
    dev_id: Sequelize.INTEGER,
    data: Sequelize.STRING,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'hr_detail'
});

const SleepDetail = sequelize.define('SleepDetail', {
    dev_id: Sequelize.INTEGER,
    data: Sequelize.STRING,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'sleep_detail'
})

const Temperature = sequelize.define('Temperature', {
    dev_id: Sequelize.INTEGER,
    data: Sequelize.STRING,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'temperature'
})

const HeartRateMeasure = sequelize.define('hr_measure', {
    dev_id: Sequelize.INTEGER,
    data: Sequelize.STRING,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'hr_measure'
})

const HRV = sequelize.define('hrv', {
    dev_id: Sequelize.INTEGER,
    hrv: Sequelize.INTEGER,
    pressure: Sequelize.INTEGER,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'hrv'
})

const StepDetail = sequelize.define('StepDetail', {
    dev_id: Sequelize.INTEGER,
    kcal: Sequelize.FLOAT,
    distance: Sequelize.FLOAT,
    data: Sequelize.STRING,
    steps: Sequelize.INTEGER,
    date: Sequelize.DATE,
}, {
    timestamps: false,
    tableName: 'step_detail'
})

const StepPerDay = sequelize.define('StepPerDay', {
    dev_id: Sequelize.INTEGER,
    date: Sequelize.DATE,
    kcal: Sequelize.FLOAT,
    distance: Sequelize.FLOAT,
    steps: Sequelize.INTEGER,
    target: Sequelize.INTEGER,
    fastSportTime: Sequelize.INTEGER,
    sportTime: Sequelize.INTEGER,
}, {
    timestamps: false,
    tableName: 'step_per_day'
})

const sync = () => {
    sequelize.sync()
}

const addForeignKey = () => {
    HeartRateDetail.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    HeartRateMeasure.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    SleepDetail.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    Temperature.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    HRV.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    StepDetail.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
    StepPerDay.hasOne(Device, { foreignKey: 'id', sourceKey: 'dev_id' })
}

module.exports = {
    Gateway,
    Device,
    HeartRateDetail,
    HeartRateMeasure,
    SleepDetail,
    Temperature,
    HRV,
    StepDetail,
    StepPerDay,
    addForeignKey,
    sync
}